$(function() {
	var sourceMapping = driver.sourceMapping;
	var targetMapping = driver.targetMapping;
	//组合
	var $conditionCombination = $("#conditionCombination");
	//属性
	var $conditionSourceField = $("#conditionSourceField");
	//表达式
	var $conditionOperator = $("#conditionOperator");
	//参数
	var $conditionArg = $("#conditionArg");
	//列表
	var $conditionList = $("#conditionList");
	//添加按钮
	var $conditionAdd = $("#conditionAdd");
	//删除按钮
	var $conditionDelete = $(".conditionDelete");

	//回显基本属性作为条件
	initConditionColumn($conditionSourceField, sourceMapping.config.column);
	//初始化已有条件列表
	initConditionList($conditionList, driver.query.and, driver.query.or);
	//绑定添加事件
	$conditionAdd.click(function(){
		var conditionCombination = $conditionCombination.find(":selected").val();
		var conditionSourceField = $conditionSourceField.find(":selected").val();
		var conditionOperator = $conditionOperator.find(":selected").val();
		var conditionArg = $conditionArg.val();
		
		//对迁移配置表单进行必填校验
		if($("#migrationConfigContainer").formValidate()==false){
			return false;
		}
		
		addCondition($conditionList, conditionCombination, conditionSourceField, conditionOperator, conditionArg);
		//重新绑定按钮删除事件
		bindEventForDeleteCondition($conditionList, $(".conditionDelete"));
		//清空参数
		$conditionArg.val('');
	});
	
});

//初始化同步属性数据
function initConditionList($conditionList, ands, ors){
	var html = '';
	//遍历AND条件列表
	var aLen = ands.length;
	for (var i = 0; i < aLen; i++) {
		var name = ands[i]['name'];
		var operator = ands[i]['operator'];
		var value = ands[i]["value"];
		var tr = '';
		html += "<tr>";
		html += "<td name='and' >and</td>";
		html += "<td name='"+name+"' >"+name+"</td>";
		html += "<td name='"+operator+"' >"+DriverOperType[operator]+"</td>";
		html += "<td name='"+value+"' >"+value+"</td>";
		html += "<td><a class='fa fa-remove fa-2x conditionDelete fastsyncer_pointer' title='删除' ></a></td>";
		html += "</tr>";
		html += tr;
	}

	//遍历AND条件列表
	var oLen = ors.length;
	for (var i = 0; i < oLen; i++) {
		var name = ors[i]['name'];
		var operator = ors[i]['operator'];
		var value = ors[i]["value"];
		var tr = '';
		html += "<tr>";
		html += "<td name='or' >or</td>";
		html += "<td name='"+name+"' >"+name+"</td>";
		html += "<td name='"+operator+"' >"+DriverOperType[operator]+"</td>";
		html += "<td name='"+value+"' >"+value+"</td>";
		html += "<td><a class='fa fa-remove fa-2x conditionDelete fastsyncer_pointer' title='删除' ></a></td>";
		html += "</tr>";
		html += tr;
	}
	//将组装好的数据渲染给页面
	$conditionList.html(html);
	//删除条件属性按钮事件
	bindEventForDeleteCondition($conditionList, $(".conditionDelete"));
	//生成预览过滤条件
	createPreviewCommond($conditionList);
}

//初始化条件属性
function initConditionColumn($this, columns){
	//去重
	var removeRepeatedArr = {};
	var clen = columns.length;
	if(clen>0){
		for (var i = 0; i < clen; i++) {
			var col = columns[i];
			removeRepeatedArr[col['name']] = col;
		}
	}
	var opts = "";
	$.each(removeRepeatedArr,function(i, col){ 
		var name = col['name'];
		var type = col['type'];
		var typeName = col['typeName'];
		opts += "<option value='"+ name +"' type='"+ type +"' typeName='"+ typeName +"' >"+ name +" ("+typeName+")</option>";
	});
	$this.html(opts);
}

//添加条件按钮事件
function addCondition($conditionList, conditionCombination, conditionSourceField, conditionOperator, conditionArg){
	var html = '';
	html += "<tr>";
	html += "<td name='"+conditionCombination+"' >"+conditionCombination+"</td>";
	html += "<td name='"+conditionSourceField+"' >"+conditionSourceField+"</td>";
	html += "<td name='"+conditionOperator+"' >"+DriverOperType[conditionOperator]+"</td>";
	html += "<td name='"+conditionArg+"' >"+conditionArg+"</td>";
	html += "<td><a class='fa fa-remove fa-2x conditionDelete fastsyncer_pointer' title='删除' ></a></td>";
	html += "</tr>";
	
	if("and"==conditionCombination){
		$conditionList.prepend(html);
	}else if("or"==conditionCombination){
		$conditionList.append(html);
	}
	//生成预览过滤条件
	createPreviewCommond($conditionList);
}

//删除同步属性按钮事件
function bindEventForDeleteCondition($conditionList, $conditionDelete){
	//删除同步属性按钮事件
	$conditionDelete.click(function(){
		var tr = $(this).parent().parent();
		tr.remove();
		//刷新过滤条件预览
		createPreviewCommond($conditionList);
	});
}

//生成预览过滤条件
function createPreviewCommond($conditionList){
	//预览过滤条件
	var ands = [];
	var ors = [];
	var trs = $conditionList.find("tr");
	trs.each(function(i,o){
		var combination = $(this).find("td:eq(0)").text();
		var name = $(this).find("td:eq(1)").text();
		var operator = $(this).find("td:eq(2)").text();
		var value = $(this).find("td:eq(3)").text();
		if("and"==combination){
			ands.push({"name": name,"operator": operator,"value": value});
		}else if("or"==combination){
			ors.push({"name": name,"operator": operator,"value": value});
		}
	});
	var conditionCMD = '';
	var aLen= ands.length;
	var oLen= ors.length;
	//并且语法
	if(aLen > 0){
		var andCommond = '(';
		for (var i = 0; i < aLen; i++) {
			var f = ands[i];
			var name = f['name'];
			var operator = f['operator'];
			var value = f['value'];
			andCommond += name + operator +value;
			andCommond += i < aLen - 1 ? " and " : "";
		}
		andCommond += ')'
		conditionCMD += andCommond;
	}
	//或者语法
	if(oLen > 0){
		var orCommond = '(';
		for (var i = 0; i < oLen; i++) {
			var f = ors[i];
			var name = f['name'];
			var operator = f['operator'];
			var value = f['value'];
			orCommond += name + operator +value;
			orCommond += i < oLen - 1 ? " or " : "";
		}
		orCommond += ')';;
		conditionCMD += conditionCMD != '' ? " or " + orCommond : orCommond;
	}
	var conditionCMD = aLen>0 || oLen>0 ? 'where ' + conditionCMD : '';
	$("#conditionPreview").html(conditionCMD);
}